package com.jamlu.framework.album;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.ViewStub;
import android.widget.ProgressBar;
import android.widget.TextView;


import com.jamlu.framework.R;

import java.util.ArrayList;
import java.util.List;

/**
 *
 * Created by DMing on 2018/1/18.
 */

public class PhotoActivity extends AppCompatActivity {

    private AlbumAdapter albumAdapter;
    private PickerManager pickerManager;
    private List<AlbumBean> albumBeanList = new ArrayList<>();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_photo);
        pickerManager = new PickerManager(this);
        RecyclerView rvAlbum = findViewById(R.id.rvAlbum);
        rvAlbum.setLayoutManager(new GridLayoutManager(this, 3));
        albumAdapter = new AlbumAdapter(albumBeanList);
        rvAlbum.addItemDecoration(new GridDivider(this,2));
        rvAlbum.setAdapter(albumAdapter);
        final ProgressBar mPbAlbum = findViewById(R.id.pbAlbum);
        pickerManager.setManagerCallBack(new PickerManager.ManagerCallBack() {
            @Override
            public void callBack() {
                mPbAlbum.setVisibility(View.GONE);
                setFolderInfoList();
            }
        });
        albumAdapter.setOnItemClickListener(new AlbumAdapter.OnItemClickListener() {
            @Override
            public void onFolderClick(List<ImageInfo> imageInfos) {
                setImageInfoList(imageInfos);
            }

            @Override
            public void onSelectImage(String path) {
                setResult(Activity.RESULT_OK,new Intent().putExtra("path",path));
                finish();
            }
        });
        initTitle();
    }


    private void setFolderInfoList(){
        albumBeanList.clear();
        List<FolderInfo> folderInfos = pickerManager.getAllFolderInfoList();
        for (FolderInfo folderInfo:folderInfos){
            albumBeanList.add(new AlbumBean(folderInfo,AlbumAdapter.FOLDER));
        }
        albumAdapter.notifyDataSetChanged();
    }

    private void setImageInfoList(List<ImageInfo> imageInfos){
        albumBeanList.clear();
        for (ImageInfo imageInfo:imageInfos){
            albumBeanList.add(new AlbumBean(imageInfo,AlbumAdapter.IMAGE));
        }
        albumAdapter.notifyDataSetChanged();
    }
    @SuppressLint("RestrictedApi")
    private void initTitle(){
        Toolbar mToolbar = findViewById(R.id.toolbar);
        mToolbar.setTitle("");
        mToolbar.setNavigationIcon(R.drawable.ic_back_white); //设置返回按钮图标
        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (albumBeanList.size() > 0 && albumBeanList.get(0).getType() == AlbumAdapter.IMAGE) {
                    setFolderInfoList();
                }else {
                    finish();
                }
            }
        });
        View title = findViewById(R.id.vs_title);
        if (title instanceof ViewStub) {
            ViewStub titleSub = (ViewStub) title;
            titleSub.setLayoutResource(R.layout.layout_title_textview);
            titleSub.inflate();
            TextView mTvTitle = findViewById(com.jamlu.framework.R.id.tv_title);
            mTvTitle.setText(R.string.txt_album);
        }
    }
}
